package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglProgressComparisonMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglMonthBuildPlan;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglProgressComparison;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglProgressComparisonService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglProgressComparisonServiceImpl implements TTgyglProgressComparisonService {

    @Autowired
    private TTgyglProgressComparisonMapper tTgyglProgressComparisonMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return 0;
    }

    @Override
    public int insert(TTgyglProgressComparison record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglProgressComparison record) {
        return 0;
    }

    @Override
    public TTgyglProgressComparison selectByPrimaryKey(String id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglProgressComparison record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglProgressComparison record) {
        return 0;
    }

    @Override
    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        //先删除对应的数据
        tTgyglProgressComparisonMapper.deleteByNfAndFf(year,month);

        List<JXNode> trNodeList = jxNode.sel("//tr");

        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            if (index > 1) {
                List<JXNode> tdNodeList = item.sel("//td");
                TTgyglProgressComparison tTgyglProgressComparison = new TTgyglProgressComparison();

                //年
                tTgyglProgressComparison.setYear(year);
                //月
                tTgyglProgressComparison.setMonth(month);
                //文件id
                tTgyglProgressComparison.setFileId(id);

                //第一行遇到'合计就跳过'
                JXNode node = tdNodeList.get(0).selOne("//p//text()");
                if (node == null || (node.toString().contains("合") && node.toString().contains("计"))) {
                    continue;
                }
                //地名
                tTgyglProgressComparison.setSectionName(node.toString());
                //线路长度
                tTgyglProgressComparison.setLength(
                        tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString());
                //杆塔数量（基）
                tTgyglProgressComparison.setRodNumber(
                        tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());
                //基础浇筑计划累计-计划完成
                tTgyglProgressComparison.setPouringPlanComplete(
                        tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
                //基础浇筑计划累计-完成比例
                tTgyglProgressComparison.setPouringPlanScale(
                        tdNodeList.get(4).selOne("//p//text()") == null ? "" : tdNodeList.get(4).selOne("//p//text()").toString());
                //基础浇筑完成累计-计划完成
                tTgyglProgressComparison.setPouringCompleteComplete(
                        tdNodeList.get(5).selOne("//p//text()") == null ? "" : tdNodeList.get(5).selOne("//p//text()").toString());
                //基础浇筑完成累计-完成比例
                tTgyglProgressComparison.setPouringCompleteScale(
                        tdNodeList.get(6).selOne("//p//text()") == null ? "" : tdNodeList.get(6).selOne("//p//text()").toString());
                //进度比较（“+”表示多于计划，“-”表示少于计划）
                tTgyglProgressComparison.setProgressComparison(
                        tdNodeList.get(7).selOne("//p//text()") == null ? "" : tdNodeList.get(7).selOne("//p//text()").toString());

                //保存数据
                tTgyglProgressComparisonMapper.insert(tTgyglProgressComparison);

                System.out.println(item);
            }

        }
    }

    @Override
    public List<TTgyglProgressComparison> selectListByFileId(Integer fileId) {
        List<TTgyglProgressComparison> resulList = null;
        TTgyglProgressComparison tTgyglProgressComparison = new TTgyglProgressComparison();
        tTgyglProgressComparison.setFileId(fileId);
        try {
            resulList = tTgyglProgressComparisonMapper.selectByBean(tTgyglProgressComparison);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
